var router = require('express').Router();
var util = require('util');
var EventProxy = require("eventproxy");
var async = require("async");

var Consts = require('../consts');
var resultJson = require('../result-json');
var Utils = require('../utils');
var logger = require('../models/logger');
var Teacher = require('../models/Teacher');
var User = require('../models/User');
var Course = require('../models/Course');

//添加修改讲师信息
router.post("/upsert", function(req, res) {
	var teacher = req.body;
	teacher = JSON.stringify(teacher);
	teacher = teacher.replace(/\</g, '&lt;').replace(/\>/g, '&gt;').replace(/</g, '&lt;').replace(/>/g, '&lg;');
	teacher = JSON.parse(teacher);

	if (teacher._id) {
		Teacher.update({
			_id: teacher._id
		}, {
			$set: teacher
		}).exec(function(err) {
			if (err) {
				logger.error(err);
				return res.send(resultJson(2));
			}
			res.send(resultJson(0));
		});
	} else {
		teacher.owner = teacher._user._id;
		new Teacher(teacher).save(function(err, teacher) {
			if (err) {
				logger.error(err);
				return res.send(resultJson(2));
			}
			res.send(resultJson(0, teacher));
		});
	}
});

//依据供应商id查询讲师信息
router.post("/list", function(req, res) {
	var proxy = new EventProxy();
	proxy.all("results", "count", function(results, count) {
		res.send(resultJson(0, {
			results: results,
			count: count
		}))
	})
	var start = req.body.start || 0;
	var count = req.body.count || Consts.MAX;
	var result = [];
	var query;
	if (req.body.owner) {
		query = Teacher.find({
			owner: req.body.owner
		});
	} else
		query = Teacher.find();

	query.skip(start).limit(count).exec(function(err, teachers) {
		if (err) {
			logger.error(err);
			return res.send(resultJson(2));
		}
		teachers = JSON.parse(JSON.stringify(teachers));
		async.eachSeries(teachers, function(item, cb) {
			User.findOne({
				_id: item.owner
			}, {
				_id: 1,
				name: 1
			}).exec(function(err, user) {
				if (err)
					cb(err);
				if (!user)
					user = {};

				item.user = user;
				result.push(item);
				cb();
			});
		}, function(err) {
			if (err) {
				logger.error(err);
				return res.send(resultJson(2));
			}
			proxy.emit("results", result);
		});
	});
	query.count(function(err, num) {
		if (err) {
			logger.error(err);
			return res.send(resultJson(2));
		}
		proxy.emit("count", num);
	});
});

//依据讲师id查询讲师信息
router.post("/detail", function(req, res) {
	var query;
	if (!req.body._id)
		return res.send(resultJson(3));

	Teacher.findById(req.body._id).exec(function(err, teacher) {
		if (err) {
			logger.error(err);
			return res.send(resultJson(2));
		}
		if (!teacher)
			return res.send(resultJson(0));

		var teacher = JSON.parse(JSON.stringify(teacher));
		User.findById(teacher.owner).exec(function(err, user) {
			if (err) {
				logger.error(err);
				return res.send(resultJson(2));
			}
			if (!user)
				user = {};

			teacher.user = user;
			Course.find({
				"teachers._id": req.body._id
			}).exec(function(err, courses) {
				console.log("courses", courses);
				if (err) {
					logger.error(err);
					return res.send(resultJson(2));
				}
				teacher.courses = courses;
				res.send(resultJson(0, teacher));
			})
		});
	});
});

//删除讲师信息
router.post("/remove", function(req, res) {
	Teacher.remove({
		_id: req.body._id
	}).exec(function(err) {
		if (err) {
			logger.error(err);
			return res.send(resultJson(2));
		}
		res.send(resultJson(0));
	})
});

//查询老师教授的课程
router.post("/listCourse", function(req, res) {
	Course.find({
			"teachers": req.body._id
		})
		.select("_id name desc photo create submit publish owner status rejectDes proportion validity teachers")
		.exec(function(err, courses) {
			if (err) {
				logger.error(err);
				return res.send(resultJson(2));
			}
			res.send(resultJson(0, courses));
		});
})

module.exports = router;